﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Protected_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnAddTeacherRole_Click(object sender, EventArgs e)
    {
        string userId = DropDownListUsers.SelectedValue;
        string userName = DropDownListUsers.SelectedItem.Text;

        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnString);
        myConnection.Open();

        // Insert into database
        try
        {
            SqlCommand cmd = new SqlCommand("aspnet_UsersInRoles_AddUsersToRoles", myConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@ApplicationName", "/"));
            cmd.Parameters.Add(new SqlParameter("@UserNames", userName));
            cmd.Parameters.Add(new SqlParameter("@RoleNames", "Teacher"));
            cmd.Parameters.Add(new SqlParameter("@CurrentTimeUtc", DateTime.Now));

            cmd.ExecuteNonQuery();
            lblResult.Text = "The teacher role was successfully added to user: " + userName;
        }
        catch (Exception ex)
        {
            lblResult.Text = "There was an error when attempting to add the teacher role to user: " + userName;
        }

        myConnection.Close();
    }
    protected void btnAddStudentRole_Click(object sender, EventArgs e)
    {
        string userId = DropDownListUsers.SelectedValue;
        string userName = DropDownListUsers.SelectedItem.Text;

        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnString);
        myConnection.Open();

        // Insert into database
        try
        {
            SqlCommand cmd = new SqlCommand("aspnet_UsersInRoles_AddUsersToRoles", myConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@ApplicationName", "/"));
            cmd.Parameters.Add(new SqlParameter("@UserNames", userName));
            cmd.Parameters.Add(new SqlParameter("@RoleNames", "Student"));
            cmd.Parameters.Add(new SqlParameter("@CurrentTimeUtc", DateTime.Now));

            cmd.ExecuteNonQuery();
            lblResult.Text = "The student role was successfully added to user: " + userName;
        }
        catch (Exception ex)
        {
            lblResult.Text = "There was an error when attempting to add the student role to user: " + userName;
        }

        myConnection.Close();
    }
    protected void btnReturnToAdminSite_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/");
    }
    protected void btnRemoveTeacherRole_Click(object sender, EventArgs e)
    {
        string userId = DropDownListUsers.SelectedValue;
        string userName = DropDownListUsers.SelectedItem.Text;

        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnString);
        myConnection.Open();

        // Insert into database
        try
        {
            SqlCommand cmd = new SqlCommand("aspnet_UsersInRoles_RemoveUsersFromRoles", myConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@ApplicationName", "/"));
            cmd.Parameters.Add(new SqlParameter("@UserNames", userName));
            cmd.Parameters.Add(new SqlParameter("@RoleNames", "Teacher"));

            cmd.ExecuteNonQuery();

            lblResult.Text = "The teacher role was successfully removed from user: " + userName;
        }
        catch (Exception ex)
        {
            lblResult.Text = "There was an error when attempting to remove the teacher role to user: " + userName;
        }

        myConnection.Close();
    }
    protected void btnRemoveStudentRole_Click(object sender, EventArgs e)
    {
        string userId = DropDownListUsers.SelectedValue;
        string userName = DropDownListUsers.SelectedItem.Text;

        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnString);
        myConnection.Open();

        // Insert into database
        try
        {
            SqlCommand cmd = new SqlCommand("aspnet_UsersInRoles_RemoveUsersFromRoles", myConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@ApplicationName", "/"));
            cmd.Parameters.Add(new SqlParameter("@UserNames", userName));
            cmd.Parameters.Add(new SqlParameter("@RoleNames", "Student"));

            cmd.ExecuteNonQuery();

            lblResult.Text = "The student role was successfully removed from user: " + userName;
        }
        catch (Exception ex)
        {
            lblResult.Text = "There was an error when attempting to remove the student role to user: " + userName;
        }

        myConnection.Close();
    }
    protected void btnRemoveUser_Click(object sender, EventArgs e)
    {
        string userId = DropDownListUsers.SelectedValue;
        string userName = DropDownListUsers.SelectedItem.Text;

        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnString);
        myConnection.Open();

        // Insert into database
        try
        {
            SqlCommand cmd = new SqlCommand("aspnet_Users_DeleteUser", myConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@ApplicationName", "/"));
            cmd.Parameters.Add(new SqlParameter("@UserName", userName));
            cmd.Parameters.Add(new SqlParameter("@TablesToDeleteFrom", 15));
            cmd.Parameters.Add(new SqlParameter("@NumTablesDeletedFrom", ""));

            cmd.ExecuteNonQuery();

            lblResult.Text = "The following user was successfully removed: " + userName;
        }
        catch (Exception ex)
        {
            lblResult.Text = "There was an error when attempting to remove the user: " + userName;
        }

        myConnection.Close();
    }
}